﻿using System;
using Microsoft.Xna.Framework;

namespace SpaceGame.Gameplay.AI.Pathfinding
{
    public class ObstaclesDensityPathHeuristic : IPathHeuristic
    {
        /// <summary>
        /// Need reference to world to sense environment.
        /// </summary>
        private readonly World world;

        /// <summary>
        /// Sensitivity factor when detecting obstacles.
        /// </summary>
        private const float radarFactor = 2;

        public ObstaclesDensityPathHeuristic(World world)
        {
            if(world == null) throw new ArgumentNullException("world");
            this.world = world;
        }

        /// <summary>
        /// Try to measure amount of asteroids in the path. Currently it calculates the sum of radius / mass of
        /// all obstacles in the way.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public float EstimatePathCost(Vector2 from, Vector2 to)
        {
            float totalDensity = 0;

            //check asteroids
            foreach (IEntity actor in world.Entities)
            {
                if (!(actor is Asteroid)) continue;
                if (Collision.CircleLineCollide(actor.Position, actor.Radius * radarFactor, from, to))
                {
                    totalDensity += (actor.Radius / actor.Mass);
                }
            }

            return totalDensity;
        }
    }
}
